#include "common.hpp"

Student input()
{
    Student stu;
    cout << "Enter student name: ";
    cin >> stu.name;
    cout << "Enter student gender: ";
    cin >> stu.gender;
    cout << "Enter student age: ";
    cin >> stu.age;
    cout << "Enter student classinfo: ";
    cin >> stu.classinfo;

    return stu;
}

bool find(char* text)
{
    FILE* fp = fopen(stupath.c_str(), "r");
    char buff[64];
    while(fgets(buff, sizeof(buff), fp))
    {
        if(strncmp(buff, text, strlen(text)) == 0)
        {
            return true;
        }
    }
    fclose(fp);
    return false;
}

void processCommand(char* command) 
{
    while (true) 
	{
        FILE* fp = fopen(stupath.c_str(), "wt+");

        if (strcmp(command, "add") == 0) 
		{
            Student stu = input();
            FILE* fp = fopen(stupath.c_str(), "wt+");
            fprintf(fp, "name: %s gender: %s age: %d classinfo: %s\n", 
            stu.name.c_str(), stu.gender.c_str(), stu.age, stu.classinfo.c_str());
            fclose(fp);

            break;
        }
		else if (strcmp(command, "delete") == 0) 
		{
            Student stu = input();
            char* text;
            sprintf(text, "name: %s gender: %s age: %d classinfo: %s\n", 
            stu.name.c_str(), stu.gender.c_str(), stu.age, stu.classinfo.c_str());

            if (find(text))
			{
                FILE* fp = fopen("student.txt", "r+");
                FILE* fp1 = fopen("tmp.txt", "w+"); // 读写新建一个临时文件

                char buff[64];
                while(fgets(buff, sizeof(buff), fp))
                {
                    if(strncmp(buff, text, strlen(text)) != 0)
                    {
                        fputs(buff, fp1);
                    }
                }
                fclose(fp);
                fclose(fp1);
                remove("students.txt");                
                rename("tmp.txt", "students.txt");
                cout << "Student deleted successfully." << endl;
                break;
            }
            else 
            {
                cout << "Student not found." << endl;
                break;
            }
        } 
		else if (strcmp(command, "query") == 0) 
		{
            Student stu = input();

            char* text;
            sprintf(text, "name: %s gender: %s age: %d classinfo: %s\n", 
            stu.name.c_str(), stu.gender.c_str(), stu.age, stu.classinfo.c_str());
            if (!find(text))
			{
                cout << "Student not found." << endl;
                break;
            }
            else
            {
                cout << "Student founded:" << endl;
                cout << text << endl;
                break;
            }
        } 
		else if (command == "exit") 
		{
			cout << "Exiting program." << endl;
            break;
        }
    }
}

int main() 
{
    int fd = open(PipeName.c_str(), O_RDONLY);
    char* buff;
    read(fd, buff, sizeof(buff));

    processCommand(buff);

    close(fd);
    unlink(PipeName.c_str());

    return 0;
}